package com.qin.EducationalManger.teacher.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.qin.EducationalManger.student.model.StudentModel;
import com.qin.EducationalManger.student.service.impl.StudentServiceImpl;
import com.qin.EducationalManger.teacher.model.TeacherModel;
import com.qin.EducationalManger.teacher.service.Imp.TeacherServiceImp;

/**
 * Servlet implementation class TeacherServlet
 */
@WebServlet("/TeacherServlet")
public class TeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TeacherServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		
		 Object res ="";
		 request.setCharacterEncoding("UTF-8");
		 response.setCharacterEncoding("UTF-8");
		  
	
	
	    switch (request.getParameter("action")) {
//		case "查询":
//			res= select(request);
//		   
//			
//			response.getWriter().write(new JSONArray((Collection<StudentModel>) res).toString());
//			break;
		case "删除":
			String de = delete(request)+"";
			
			response.getWriter().write(de);
			
			break;
		case "添加":
			String ree=  add(request)+"";
			response.getWriter().write(ree);
			break;
		case "修改":
		   
		String upr = update(request)+"";
		  response.getWriter().write(upr);  
		   break;
		case"查询":
			Map<String, Object> rec = page(request);
		   
			
			response.getWriter().write(new JSONObject(rec).toString());
			
			break;
			
		case"删除课程信息":
		
			 String red = deleteCourse(request).toString();
			 response.getWriter().write(red);
			
			break;
		
			
		default:
			break;
		}
		
	

	}		
	

		//删除单一课程
	private Object deleteCourse(HttpServletRequest request) {
		// TODO Auto-generated method stub
		String studentId =request.getParameter("studentId");
		String courseId =request.getParameter("courseId");
		

		return  service.updataCourse(studentId,courseId);
	}

	//分页
	private Map<String, Object> page(HttpServletRequest request) {
		// TODO Auto-generated method stub
		String teacherId = request.getParameter("teacherId");	
		
	
		String teacherName = request.getParameter("teacherName");
		
		String pageIndex = request.getParameter("pageIndex");
		String pageLimit = request.getParameter("pageLimit");
		
		TeacherModel model = new TeacherModel();
		model.setTeacherId(teacherId);
		model.setTeacherName(teacherName);
		
		if(pageIndex==null) {
			return null;
		}
		
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		List<TeacherModel> models = service.select(model);
		System.out.println(models.get(0).toString());
		
		int count = service.selectCount(model);
		
		
	
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", models);
		map.put("count", count);
		
		return map;
		
		
	
	}

	private Integer update(HttpServletRequest request) {
		// TODO Auto-generated method stub
	
	
		
		String teacherId= request.getParameter("teacherId");
		
	
		String teacherName = request.getParameter("teacherName");
   

		String teacherSex = request.getParameter("teacherSex");
		String title = request.getParameter("title");
		String tel = request.getParameter("tel");
		String courseIds[] = request.getParameterValues("courseId");
		
		String courseId ="";
		for(String i:courseIds) {
			
			System.out.println("测试"+i);
			courseId+=i+" ";
		}
	
		
		
		TeacherModel  model = new TeacherModel(teacherId,teacherName,teacherSex,title,courseId,tel);
		
		
	 return	service.update(model);
		
		
	}
	
	private TeacherServiceImp service = new TeacherServiceImp();
//添加
	    private int add(HttpServletRequest request) {
		// TODO Auto-generated method stub
	    	TeacherModel model = new TeacherModel();
	    	String teacherId =request.getParameter("teacherId");
	        model.setTeacherId(teacherId);
	       
	        
	    	List<TeacherModel> models = service.select(model);//先查询数据库中是否已经存在该学号
	    	
	    
	    	
	    	if(models.isEmpty()) {  //如果不存在，再往数据库插入
	        		
	    	String teacherName =request.getParameter("teacherName");
	    	String teacherSex = request.getParameter("teacherSex");
	    	
	    	
	    	String title =request.getParameter("title");
	    	
	    	String []courseIds =request.getParameterValues("courseId");
	   
	    	String courseId ="";
	    	
	    	for(String i:courseIds) {
	    		System.out.println("测试     "+i );
	    		courseId+=i+" ";
	    	}
	    	  System.out.println("测试213 行   courseId"+courseId);
	    
	    	
	    	String tel =request.getParameter("tel");
	    	model.setTeacherId(teacherId);
	    	model.setTeacherName(teacherName);
	    	model.setTeacherSex(teacherSex);
	    	model.setCourseId(courseId);
	    	model.setTitle(title);
	    	model.setTel(tel);
	    	
	    	
	    	return service.insert(model);
	    	}else {
				return 0;
			}
	    	
		
	}

		private int delete(HttpServletRequest request) {
		// TODO Auto-generated method stub
	    	String teacherId = request.getParameter("teacherId");
	    	
	    	
	    	
	    	TeacherModel model =  new TeacherModel();  
	    	
	    	model.setTeacherId(teacherId);;
	    	
	    	return service.delete(model);
	    	
	    	
	    	
	
	}


}
